import { createRouter, createWebHistory } from 'vue-router'

import Friend from '@/components/friends/Friend.vue'
import Login from '@/components/axios/Login.vue'
import GoodsShow2 from '@/components/todos/GoodsShow2.vue'
import GoodsShow from '@/components/GoodsShow.vue'
import TodoList from '@/components/todos/TodoList.vue'
import Students from '@/components/students/StudentList.vue'
import WeatherApp from '@/components/todos/WeatherApp.vue'
import About from '@/components/nestedrouter/About.vue'
import NewGoods from '@/components/nestedrouter/NewGoods.vue'
import HotGoods from '@/components/nestedrouter/HotGoods.vue'
import NestRoute from '@/components/nestedrouter/NestRoute.vue'
import Composed from '@/components/Composed.vue'
import Cout from '@/components/todos/Cout.vue'
import Shopcart from '@/components/shopcart/Shopcart.vue'
import List from '@/components/shopcart/List.vue'
import Cart from '@/components/shopcart/Cart.vue'
//创建路由器
const router = createRouter({
  //history模式,webhash,webhistory
  history: createWebHistory(),
  //路由表
  routes: [
    {
      //根路径
      path: '/',
      //component: TodoList,
      redirect: '/login',
    },
    {
      path: '/sc',
      component: Shopcart,
      children: [
        { path: '/sc/list', component: List },
        {
          path: '/sc/cart',
          component: Cart,
        },
      ],
    },
    {
      path: '/cout',
      component: Cout,
    },
    {
      path: '/c',
      component: Composed,
    },
    {
      path: '/login',
      component: Login,
      name: 'login',
    },
    {
      path: '/goodsshow2',
      component: GoodsShow2,
    },
    {
      path: '/goodsshow',
      component: GoodsShow,
    },
    {
      path: '/todo',
      component: TodoList,
      name: 'todo',
      meta: {
        title: '待办事项',
        requiredAuth: true,
      },
    },
    {
      path: '/students',
      component: Students,
    },
    {
      path: '/w/:cityName',
      component: WeatherApp,
      name: 'weather',
      meta: {
        title: '天气信息',
        requiredAuth: true,
      },
    },
    {
      path: '/friend',
      component: Friend,
    },
    {
      path: '/about',
      component: About,
    },
    {
      path: '/new',
      component: NewGoods,
    },
    {
      path: '/hot',
      component: HotGoods,
    },
    {
      path: '/nest',
      component: NestRoute,
      name: 'nest',
      meta: {
        title: '嵌套路由',
        requiredAuth: true,
      },
      children: [
        {
          path: 'about',
          name: 'about',
          meta: {
            title: '关于我们',
          },
          component: About,
        },
        {
          path: 'new',
          name: 'new',
          meta: {
            title: '最新商品',
            requiredAuth: true,
          },
          component: NewGoods,
        },
        {
          path: 'hot',
          name: 'hot',
          meta: {
            title: '热门商品',
          },
          component: HotGoods,
        },
      ],
    },
  ],
})
router.beforeEach((to, form, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  if (to.matched.some((item) => item.meta.requiredAuth == true)) {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next({
        name: 'login',
        query: {
          path: to.fullPath,
        },
      })
    }
  } else {
    next()
  }
})
//输出路由器
export default router
